<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use front\assets\AppAsset;
use yii\helpers\Url;
?>
<body class="u-bg1">
<div class="m-odr-bar f-cb" id="J_sltBar">
	<span <?php if(empty($type)){echo 'class="z-on"';}?> >全部订单</span>
	<span <?php if($type==1){echo 'class="z-on"';}?>>待付款</span>
	<span <?php if($type==2){echo 'class="z-on"';}?>>待收货</span>
	<span <?php if($type==3){echo 'class="z-on"';}?>>待评价</span>
</div>

<div class="f-cb m-odr-cnt" id="J_sltCnt">
	<div>
        <?php if($list){ ?>
            <?php foreach($list as $k=>$v){?>
                    <ul oid="<?php echo $v['oid']?>">
                        <?php foreach($v['list'] as $key=>$val){?>
                            <li class="u-list">
                                <a href="<?= Url::to(['product/detail','pid' => $key])?>"><img src="<?php echo $val['img']?>"></a>
                                <h6><a class="u-tit" href="<?= Url::to(['product/detail','pid' =>$key])?>"><b><?php echo $val['title'];?></b><br><?php echo $val['attr']?></a></h6>
                                <em>￥ <b><?php echo $val['price'];?></b></em>

                            </li>
                        <?php } ?>
                    </ul>

                    <dl class="f-cb"><dt>订单编号：<?php echo $v['onum'];?></dt>

                        <?php if(isset($v['orderType'][0])){?>
                          <dd><a href="javascript:;"><?php echo $v['orderType'][0];?></a></dd>
                        <?php }?>

                    </dl>
                    <p>

                        <?php if($v['type']==4){?>
                            <?php if(isset($v['orderType'][2])){?><a href="javascript:;" class="cancel"><?php echo $v['orderType'][2];}?></a>
                            <?php if(isset($v['orderType'][1])){?> <a href="<?=url::to(['message/logist','order_id'=>$v['oid']])?>"><?php echo $v['orderType'][1];}?> </a>
                        <?php }elseif($v['type']==0){?>
                            <?php if(isset($v['orderType'][1])){?><a href="javascript:;" data-emptyoid="<?php echo $v['oid'];?>" class="cancel empty-order"><?php echo $v['orderType'][1];}?></a>
                            <?php if(isset($v['orderType'][2])){?> <a href="<?=url::to(['order/payment','order_id'=>$v['oid']])?>" class="order-pay"><?php echo $v['orderType'][2];}?> </a>
                        <?php }elseif($v['type']==5){?>
                            <?php if(isset($v['orderType'][1])){?><a href="javascript:;" class="cancel del-order" data-deloid="<?php echo $v['oid'];?>"><?php echo $v['orderType'][1];}?></a>
                            <?php if(isset($v['orderType'][2])){?> <a href="<?=url::to(['comment/add','order_id'=>$v['oid'],'product_id'=>$v['product_id']])?>"><?php echo $v['orderType'][2];}?> </a>
                        <?php }?>

                    </p>
            <?php } ?>
        <?php }else{ ?>
            <h4>您还没相关订单</h4>
        <?php } ?>
	</div>
	<div></div>
	<div></div>
	<div></div>
</div>


<script>

    $(function(){

        // Tab切换
        var sltBar = $('#J_sltBar').children('span'),
            sltCnt = $('#J_sltCnt').children('div'),
            zon = 'z-on';
        sltBar.on('touchend', function(evt){

            var _that = $(this);

            if(_that.hasClass(zon)) return !1;

            var _i = _that.index();

            sltBar.removeClass(zon).eq(_i).addClass(zon);

            var _html = '';

            // 动态加载数据
            $.ajax({ url: "<?=Url::to(['order/ajaxorder'])?>",dataType:'jsonp', data:{'type':_i}, timeout:5000,
                success: function(_info){
                    _html = createList(_info);
                    sltCnt.hide().eq(_i).html(_html).show();
                    emptyOrder();
                }
            });

            evt.preventDefault();
            return false;
        });

        var cancel_order,del_order;
        $('.m-odr-cnt .empty-order').each(function(){
            $(this).click(function(){
                cancel_order = $(this).attr('data-emptyoid');
                $.get("<?=url::to(['cancelorder'])?>",{cancel_order:cancel_order},function(data){
                   if(data.status=1){
                       msgPopup(data.message);
                       location.reload();
                   }
                },'jsonp')
            })
        })

        $('.m-odr-cnt .del-order').each(function(){
            $(this).click(function(){
                del_order = $(this).attr('data-deloid');
                $.get("<?=url::to(['delorder'])?>",{del_order:del_order},function(data){
                    if(data.status=1){
                        msgPopup(data.message);
                        location.reload();
                    }
                },'jsonp')
            })
        })

    });


    // 动态创建列表
    function createList(info){
        var _html = '';

        for(var i in info){

            var rs = info[i], list = rs['list'], status = rs['type'];

            var _btn = '', _tname = '';
            if(status == 0){
                _btn = '<a href="javascript:;'+ rs['oid'] +'" data-emptyoid="'+rs['oid']+'" class="cancel empty-order">取消订单</a><a href="/web/order/payment?order_id='+rs['oid']+'">付款</a>';
                _tname = '待付款';
            }else if(status == 4){
                _btn = '<a href="/web/message/logist?order_id='+ rs['oid'] +'">查看物流</a>';
                _tname = '待收货';
            }else if(status == 5){
                _btn = '<a href="javascript:;'+ rs['oid'] +'" data-deloid="'+rs['oid']+'" class="cancel del-order">删除订单</a><a href="/web/comment/add?order_id='+ rs['oid'] +'&product_id='+rs['product_id']+'">评价</a>';
                _tname = '待评价';
            }else if(status == 2){
                _tname = '已支付';
            }else if(status == 3){
                _tname = '待发货';
            }else if(status == 6){
                _tname = '已评价';
            }


            var _li = '';
            for(var k in list){

                var row = list[k];

                _li += '<li class="u-list">'+
                    '<a href="/web/product/detail?pid='+ k +'"><img src="'+ row['img'] +'"></a>'+
                    '<h6><a class="u-tit" href="/web/product/detail?pid='+k+'"><b>'+ row['title'] +'</b><br>';
                    if(row['attr']!=null) {
                _li +=row['attr'];
                    }
                _li += '</a></h6>'+
                    '<em>￥ <b>'+ row['price'] +'</b></em>'+
                    '</li>';
            }



            if(_li !== '') _html += '<ul>'+ _li +'</ul><dl class="f-cb"><dt>订单编号：'+ rs['onum'] +'' +
                '</dt><dd><a href="javascript:;">'+ _tname +'</a></dd></dl><p>'+ _btn +'</p>';
        }

        if(_html === '') _html = '<h4>您还没相关订单</h4>';

        return _html;
    }

    function emptyOrder(){
        //取消订单
       $('.m-odr-cnt .empty-order').each(function(){
           $(this).click(function(){
               cancel_order = $(this).attr('data-emptyoid');
               $.get("<?=url::to(['cancelorder'])?>",{cancel_order:cancel_order},function(data){
                   if(data.status=1){
                       msgPopup(data.message);
                       location.reload();
                   }
               },'jsonp')
           })
       })

        $('.m-odr-cnt .del-order').each(function(){
            $(this).click(function(){
                del_order = $(this).attr('data-deloid');
                $.get("<?=url::to(['delorder'])?>",{del_order:del_order},function(data){
                    if(data.status=1){
                        msgPopup(data.message);
                        location.reload();
                    }
                },'jsonp')
            })
        })

    }
</script>


